﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmPurchaseOrderApproval : Form
    {
       DataTable GvDatatable1 = new DataTable();
        DataTable GvDatatable = new DataTable();
        DataTable GvPLIDList = new DataTable();
        public frmPurchaseOrderApproval()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvPOApproval.Columns.Insert(0, check);

            DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
            check1.HeaderText = "Check Item";
            check1.FalseValue = "0";
            check1.TrueValue = "1";
            check1.Width = 20;
            dgvPOApprove.Columns.Insert(0, check1);


            #region GvDatatable-Data Table

            GvDatatable.Columns.Add("PONo ");
            GvDatatable.Columns.Add("ProductId ");
            GvDatatable.Columns.Add("ProductName ");
            GvDatatable.Columns.Add("Brand");
            GvDatatable.Columns.Add("Ret_Price ");
            GvDatatable.Columns.Add("Pur_Price ");
            GvDatatable.Columns.Add("PV ");
            GvDatatable.Columns.Add("BV ");
            GvDatatable.Columns.Add("PL_ID ");
            GvDatatable.Columns.Add("Description ");
            GvDatatable.Columns.Add("Qty");
            GvDatatable.Columns.Add("status");
            GvDatatable.Columns.Add("User_Modify");
            GvDatatable.Columns.Add("Last_Modify");
          
            #endregion


            #region GvPROIDList-Data Table

            GvPLIDList.Columns.Add("PO_ID");
            GvPLIDList.Columns.Add("PL_ID");
           
            #endregion
        }
        public void PendingPO()
        {
            try
            {
                List<Purchase_Order> LvList = new List<Purchase_Order>();
                String LvPenList = "PENDING";
                LvList = Purchase_OrderDAO.GetAllPObyStatus(LvPenList);
                DataTable LvPOPending = new DataTable();
                dgvPOApproval.DataSource = LvPOPending;

                #region LvPOPending--Datatable
                //LvPRPending.Columns.Add("Select/Unselect", (typeof(bool)));
                LvPOPending.Columns.Add("PO_ID");
                LvPOPending.Columns.Add("Sup_ID");
                LvPOPending.Columns.Add("PO_Date");
                LvPOPending.Columns.Add("Status");
                LvPOPending.Columns.Add("Branch");
                LvPOPending.Columns.Add("User_ID");
                LvPOPending.Columns.Add("Date_Create");
          
                //LvPRPending.Columns.Add("Po_Id");



                #endregion


                foreach (Purchase_Order Lvitem in LvList)
                {
                    LvPOPending = (DataTable)dgvPOApproval.DataSource;
                    LvPOPending.Rows.Add(LvPOPending.NewRow());
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][0] = Lvitem.Po_Id.ToString();
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][1] = Lvitem.Sup_Id.ToString();
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][2] = Lvitem.Po_Date.ToString();
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][3] = Lvitem.status.ToString();
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][4] = Lvitem.Branch_Id.ToString();
                    LvPOPending.Rows[LvPOPending.Rows.Count - 1][5] = Lvitem.User_Id.ToString();
                    //LvPOPending.Rows[LvPOPending.Rows.Count - 1][6] = Lvitem.Date_Create();

                   
                }


            }
            catch (Exception)
            {

            }
        }
        private void PODetailFillGrid(string PORid)
        {
            string LvStatus = "PENDING";
            GvDatatable1 = Purchase_OrderDAO.GetPoinDetailsByPoId(PORid, LvStatus);


            for (int i = 0; i <= GvDatatable1.Rows.Count - 1; i++)
            {
                GvDatatable.Rows.Add(GvDatatable.NewRow());
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][0] = GvDatatable1.Rows[i][0].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][1] = GvDatatable1.Rows[i][1].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][2] = GvDatatable1.Rows[i][2].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][3] = GvDatatable1.Rows[i][3].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][4] = GvDatatable1.Rows[i][4].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][5] = GvDatatable1.Rows[i][5].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][6] = GvDatatable1.Rows[i][6].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][7] = GvDatatable1.Rows[i][7].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][8] = GvDatatable1.Rows[i][8].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][9] = GvDatatable1.Rows[i][9].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][10] = GvDatatable1.Rows[i][10].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][11] = GvDatatable1.Rows[i][11].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][12] = GvDatatable1.Rows[i][12].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][13] = GvDatatable1.Rows[i][13].ToString();



               
               
               
            }

            dgvPOApprove.DataSource = GvDatatable;

        }     

        private void POListToApprove(string PPOID,string PLID)
        {

            GvPLIDList.Rows.Add(GvPLIDList.NewRow());
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][0] = PPOID;
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][0] = PLID;
          
        }

        private void POApprovalList(string PList)
        {
            string Lvstatus=null;
            if (rbCancel.Checked)
	{
		 Lvstatus="CANCELLED";
	}
            else
                if (rbApproved.Checked)
	{
		  Lvstatus="APPROVED";
	}
            int Lvresult = Purchase_OrderDAO.UpdatePO(PList, Lvstatus);
            if (Lvresult != 0 && Lvstatus == "APPROVED")
        {
            MessageBox.Show("Purchase Order Approved Sucessfully");
        }
            else
                 if (Lvresult != 0 && Lvstatus == "CANCELLED")
        {
            MessageBox.Show("Purchase Order Cancelled Sucessfully");
        }
        }

        private void btnPOApproval_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("podetails");

            string detailsXML = "";

            ds.Tables.Add(GvPLIDList.Copy());
            detailsXML = ds.GetXml();

            POApprovalList(detailsXML);
            MessageBox.Show("PO Was Succesfuly Saved");
            

        }

        private void frmPurchaseOrderApproval_Load(object sender, EventArgs e)
        {
            PendingPO();
        }

        private void dgvPOApproval_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvPOApproval.Rows[dgvPOApproval.CurrentRow.Index].Cells[0];

            string LvPOid = dgvPOApproval.Rows[e.RowIndex].Cells[1].Value.ToString();


            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PODetailFillGrid(LvPOid);


                    break;

            }
        }

        private void dgvPOApprove_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvPOApprove.Rows[dgvPOApprove.CurrentRow.Index].Cells[0];

            string LvPLid = dgvPOApprove.Rows[e.RowIndex].Cells[9].Value.ToString();
            //string LvStatus = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[4].Value.ToString();
            string LvPOid = dgvPOApprove.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    POListToApprove(LvPOid,LvPLid);


                    break;
            }
        }
    }
}
